<template>
  <Layout>
    <div class="main-container" v-loading="isLoading" ref="mainContainer">
      <BlogDetail v-if="data" :blog="data.data" />
      <BlogComment v-if="!isLoading" />
    </div>
    <template #right>
      <BlogTOC v-if="data" :toc="data.data.toc" />
    </template>
  </Layout>
</template>

<script>
import fetchData from "@/mixins/fetchData.js";
import { getBlog } from "@/api/blog.js";
import Layout from "@/components/Layout";
import BlogTOC from "@/views/Blog/components/BlogTOC.vue";
import BlogDetail from "@/views/Blog/components/BlogDetail.vue";
import BlogComment from "@/views/Blog/components/BlogComment.vue";
import {debounce} from "@/utils"
export default {
  mixins: [fetchData(null)],
  methods: {
    async fetchData() {
      return await getBlog(this.$route.params.id);
    },
    // 滚动条不断触发事件总线里面的方法
    handleScroll() {
      this.$bus.$emit("mainScroll", this.$refs.mainContainer);
    },
  },
  components: {
    Layout,
    BlogTOC,
    BlogDetail,
    BlogComment,
  },
  updated() {
    const hash = location.hash;
    location.hash = "";
    setTimeout(() => {
      location.hash = hash;
    }, 50);
  },
  mounted() {
    //监听滚动条变化
    const handleScrollDebounce = debounce(this.handleScroll);     //防抖函数，避免函数的多次执行
    this.$refs.mainContainer.addEventListener("scroll", handleScrollDebounce);
  },
  beforeDestroy() {
    this.$refs.mainContainer.removeEventListener("scroll", this.handleScroll);
  },
};
</script>

<style scoped lang="less">
@import "~@/styles/mixin.less";
.main-container {
  height: 100%;
  .self-scroll();
  &::-webkit-scrollbar {
    display: none;
  }
}
</style>